package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDiskDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbDiskDetailRepository extends JpaRepository<CmdbDiskDetail, Integer>, JpaSpecificationExecutor<CmdbDiskDetail> {
    List<CmdbDiskDetail> findAllByDiskDetailIdIn(List<Long> id);

    @Query(value = "select d from CmdbDiskDetail d where d.isDelete = 0 " +
            "and (:#{#paramSearchDto.diskDetailCode} is null OR d.diskDetailCode LIKE CONCAT('%', :#{#paramSearchDto.diskDetailCode}, '%') ) " +
            "and (:#{#paramSearchDto.diskStandardCode} is null OR d.diskStandardCode = :#{#paramSearchDto.diskStandardCode}) " +
            "and (:#{#paramSearchDto.sn} is null OR d.sn LIKE CONCAT('%', :#{#paramSearchDto.sn}, '%') ) " +
            "and (:#{#paramSearchDto.manufactDate} is null OR d.manufactDate = :#{#paramSearchDto.manufactDate}) " +
            "and (:#{#paramSearchDto.expirateDate} is null OR d.expirateDate = :#{#paramSearchDto.expirateDate}) " +
            "and (:#{#paramSearchDto.deviceCode} is null OR d.deviceCode = :#{#paramSearchDto.deviceCode}) " +
            "and (:#{#paramSearchDto.locationId} is null OR d.locationId = :#{#paramSearchDto.locationId}) " +
            "and (:#{#paramSearchDto.lifeStatus} is null OR d.lifeStatus = :#{#paramSearchDto.lifeStatus}) ")
    Page<CmdbDiskDetail> searchByParam(Pageable pageable, CmdbDiskDetail paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_disk_detail set is_delete = :deleted where disk_detail_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_disk_detail set is_delete = :deleted where location_id = :deviceDetailId and life_status = 1", nativeQuery = true)
    void updateIsDeleteByDeviceDetailId(Integer deleted, Integer deviceDetailId);

    List<CmdbDiskDetail> findAllBySnAndIsDelete(String sn, Integer noDelete);

    CmdbDiskDetail findByDiskDetailId(Integer diskDetailId);

    List<CmdbDiskDetail> findAllByDiskDetailCodeIn(List<String> collect);

    List<CmdbDiskDetail> findAllByDeviceCodeAndIsDelete(String deviceCode, Integer noDeleted);

}

